package com.aaa.dang.modules.checkin.service.impl;

import com.aaa.dang.common.systemlog.annotation.SysLog;
import com.aaa.dang.common.util.ResultModel;
import com.aaa.dang.modules.checkin.dao.CheckInDao;
import com.aaa.dang.modules.checkin.dao.CheckOutDao;
import com.aaa.dang.modules.checkin.service.ICheckOut;
import com.aaa.dang.modules.login.service.ILoginService;
import com.aaa.dang.modules.system.dao.IEmployeeDao;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 刘旭泽
 * @version 1.0
 * @date 2018/12/20
 * @description
 */
@Service
public class CheckOutImpl implements ICheckOut {
    @Autowired
    CheckOutDao dao;

    @Autowired
    ILoginService loginService;

    @Override
    @Transactional
    @SysLog(operateType = SysLog.Type.UPDATE,desc = "办理退宿")
    public ResultModel updateUser(Map<String, Object> query) {
        ResultModel model = new ResultModel();
        try {

            //判断该手机号是否已经办理过退宿手续
            int isDel = dao.selectExist(query);
            if (isDel == 1||String.valueOf(isDel)==null) {
                model.setCode(2);
                model.setMsg("该用户已经办理过退宿");
                return model;
            }


            //获取user id 押金 到期时间
            Map<String, Object> map = dao.selectUser(query);
            //更改 is_del 退宿时间
            int a = dao.updateUser(query);
            int id = Integer.parseInt(map.get("id") + "");
            //查询room_user_bed表 返回roomid
            Map<String, Object> map2 = dao.selectRoom(id);
            int roomid = Integer.parseInt(map2.get("roomid") + "");
            //更改room_user_bed表
            int c = dao.updateBed(id);
            //更改tbl_room表前先查询如果退宿人数是最后一人 更改房间类型
            int peopleNum = dao.selectRoomForNum(roomid);
            int d;
            if(peopleNum==1){
                d =dao.updateRoomByNum(roomid);
            }else {
                d = dao.updateRoom(roomid);
            }
            //计算出逾期时间 剩余时间 用于退房费和补交房费
            Map<String, Object> map3 = dao.selectDiffTime(query);
            //等到相差天数
            int diffdate = Integer.parseInt(map3.get("diffdate") + "");

            int payType = Integer.parseInt(map.get("payType") + "");
            int deposit = Integer.parseInt(map.get("deposit") + "");
            //免租的不退 只补  负数补缴 正数退租
            Map<String, Object> map5 = new HashMap<>();
            map5.put("deposit",deposit);
            if (payType > 0) {
                if (diffdate < 0) {
                    //补缴天数
                    map5.put("diffdate", diffdate);
                } else {
                    map5.put("diffdate", 0);
                }
            } else {
                map5.put("diffdate", diffdate);
            }
            //插入记录到退宿记录表中
            int diff = Integer.parseInt(map5.get("diffdate") + "");
            String aname = loginService.getEmployeeName();
            int e = dao.insertReturns(id,diff,aname);
            if (a > 0 && d > 0 && c > 0 && e > 0) {
                model.setMsg("退宿成功");
                model.setData(map5);
                model.setCode(0);
            } else {
                model.setMsg("退宿失败");
                model.setCode(1);
            }
        } catch (Exception e) {
            model.setCode(1);
            model.setMsg("退宿失败");
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }

        return model;
    }

    @Override
    public ResultModel getAllHistory(Map<String, Object> query) {
        ResultModel model = new ResultModel();
        int pageNum = Integer.parseInt(query.get("page") + "");
        int pageSize = Integer.parseInt(query.get("limit") + "");
        PageHelper.startPage(pageNum, pageSize);
        model.setData(dao.getAllH(query));
        model.setMsg("");
        model.setCode(0);
        model.setCount(dao.getAllHistoryCount(query));
        return model;
    }

    @Override
    public ResultModel getMsgByPhone(Map<String, Object> query) {
        ResultModel model = new ResultModel();
        Map<String,Object> map = dao.getMsgByPhone(query);
        if(map==null){
           model.setCode(1);
           model.setMsg("无该学生信息");
        }else {
            model.setCode(1);
            model.setData(map);
            model.setMsg("得到该学生信息");
        }


        return model;
    }
}
